<?php
/**
 * Created by PhpStorm.
 * User: lzy
 * Date: 2018/11/7
 * Time: 9:51
 */

namespace App\FreeBird\Repositories;

use App\FreeBird\Contracts\AutomaticInterface;
use App\FreeBird\Contracts\InfoComplaintInterface;
use App\FreeBird\Contracts\InfoInterface;
use App\FreeBird\Exceptions\FreeBirdException;
use App\FreeBird\Models\InfoAcceptlogModel;
use App\FreeBird\Models\InfoCommentModel;
use App\FreeBird\Models\InfoComplaintModel;
use App\FreeBird\Models\InfoModel;
use App\FreeBird\Models\InfoPayLogModel;
use App\FreeBird\Models\MessageModel;
use App\Pay\Repositories\DbWeixinPayRepositories;
use App\Weixin\Contracts\UserInterface;
use App\Weixin\Models\WeixinUserModel;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Request;

class  DbInfoRepositories implements InfoInterface
{

    /**
     * 获取信息模型
     * @return InfoModel
     */
    public function getInfoModel()
    {
        return app()->make(InfoModel::class);
    }

    /**
     * 订单支付号
     * @return InfoPayLogModel
     */
    public function getInfoPayLogModel()
    {
        return app()->make(InfoPayLogModel::class);
    }

    /**
     * @return AutomaticInterface
     */
    public function getAutomaticInterface()
    {
        return app()->make(AutomaticInterface::class);
    }

    /**
     * 获取投诉
     * @return InfoComplaintInterface
     */
    public function getInfoComplaintInterface()
    {
        return app()->make(InfoComplaintInterface::class);
    }

    /***
     * 获取用户信息
     * @return UserInterface
     */
    public function getUserInterface()
    {
        return app()->make(UserInterface::class);
    }

    /**
     * 获取订单评论模型
     * @return InfoModel
     */
    public function getInfoCommentModelModel()
    {
        return app()->make(InfoCommentModel::class);
    }

    /**
     * 消费模型
     * @return MessageModel
     */
    public function getMessageModel()
    {
        return app()->make(MessageModel::class);
    }

    /**
     * 支付
     * @return DbWeixinPayRepositories
     */
    public function getDbWeixinPayRepositories()
    {
        return app()->make(DbWeixinPayRepositories::class);
    }

    /**
     * 微信
     * @return WeixinUserModel
     */
    public function getWeixinUserModel()
    {
        return app()->make(WeixinUserModel::class);
    }

    /**
     * 接单模型
     * @return InfoAcceptlogModel
     */
    public function getInfoAcceptlogModel()
    {
        return app()->make(InfoAcceptlogModel::class);
    }


    /**
     * 获取订单列表
     * @param $where
     */
    public function getList($where)
    {
        $request = Request::instance();
        $limit = empty($request->input("limit")) ? 10 : $request->input("limit");
        $sort = empty($request->input("sort")) ? 'create_time' : $request->input("sort");
        $order = empty($request->input("order")) ? 'desc' : $request->input("order");
        return $this->getInfoModel()->newQuery()->where($where)->orderBy($sort, $order)->paginate($limit);
    }

    /**
     * 后台获取订单列表
     * @param $where
     * @return mixed|void
     */
    public function getListAdmin($where)
    {

        $request = Request::instance();
        $limit = empty($request->input("limit")) ? 10 : $request->input("limit");
        $sort = empty($request->input("sort")) ? 'create_time' : $request->input("sort");
        $order = empty($request->input("order")) ? 'desc' : $request->input("order");
        return $this->getInfoModel()->newQuery()->select([$this->getInfoModel()->table . ".*", $this->getWeixinUserModel()->table . ".truename", $this->getWeixinUserModel()->table . ".mobile"])->leftJoin($this->getWeixinUserModel()->table, $this->getWeixinUserModel()->table . '.uid', '=', $this->getInfoModel()->table . '.uid')->where($where)->orderBy($sort, $order)->paginate($limit);
    }

    /**
     * 保存数据
     * @param $data
     * @return \Illuminate\Database\Eloquent\Model
     */
    public function saveData($data)
    {
        if (empty($data['id'])) {
            $data['id'] = 0;
        }
        return $this->getInfoModel()->newQuery()->updateOrCreate(["id" => $data['id']], $data);
    }

    /**
     * 保存订单并返回支付信息----未完成
     * @param $data
     * @return mixed|void
     */
    public function saveDataPay($data)
    {
        $data["uid"] = AUID;
        $paySn = $this->makePaySn(AUID);
        $orderSn = $this->makeOrderSn($paySn);
        $data["order_sn"] = $orderSn;
        if ($rData = $this->saveData($data)) {
            $openId = $this->getUserInterface()->getOpenIdByUid(AUID);
            $payData = array();
            $payData["pay_sn"] = $paySn;
            $payData["order_sn"] = $orderSn;
            $payData["pay_amount"] = $rData['all_amount'];
            $payData["uid"] = AUID;
            $this->getInfoPayLogModel()->newQuery()->create($payData);
            $notify_url = get_config_param_value('XIAO_WXPAY_BING_NOTIFY');
            //返回值，生成支付信息
            return $this->getDbWeixinPayRepositories()->getWeixinJsPay($paySn, $rData['all_amount'], $openId, $notify_url, "求邦支付");
        }
    }

    /**
     * 支付订单信息
     * @param $data
     * @return mixed|void
     */
    public function getPayOrder($data)
    {
        $id = $data["id"];
        $openId = $this->getUserInterface()->getOpenIdByUid(AUID);
        $orderInfo = $this->getInfoModel()->newQuery()->where("id", $id)->where("uid", AUID)->first();
        if (empty($orderInfo)) {
            throw  new FreeBirdException(FreeBirdException::EROOR_INFO_NOT_FIND);
        }
        $paySn = $this->makePaySn(AUID);
        $payData = array();
        $payData["pay_sn"] = $paySn;
        $payData["order_sn"] = $orderInfo->order_sn;
        $payData["pay_amount"] = $orderInfo->all_amount;
        $payData["uid"] = AUID;
        $this->getInfoPayLogModel()->newQuery()->create($payData);  //生成支付信息
        $notify_url = get_config_param_value('XIAO_WXPAY_BING_NOTIFY');  //支付成功后通知地址//$orderInfo->all_amount
        return $this->getDbWeixinPayRepositories()->getWeixinJsPay($paySn, $orderInfo->all_amount, $openId, $notify_url, "求邦支付");
    }

    /**
     * 获取订单完成滚动信息
     * @param $where
     * @return array|mixed
     */
    public function getOrderInfoListLimit($where)
    {
        $request = Request::instance();
        $limit = empty($request->input("limit")) ? 10 : $request->input("limit");
        $sort = empty($request->input("sort")) ? 'create_time' : $request->input("sort");
        $order = empty($request->input("order")) ? 'desc' : $request->input("order");
        $list = $this->getInfoModel()->newQuery()->where($where)->orderBy($sort, $order)->paginate($limit);

        $returnArr = array();
        foreach ($list as $k => $v) {
            $name = empty($v->makeOrderUser->truename) ? $v->makeOrderUser->nickname : $v->makeOrderUser->truename;
            $bname = empty($v->filshOrderUser->truename) ? $v->filshOrderUser->nickname : $v->filshOrderUser->truename;
            $message = $v->is_door == 1 ? '线上服务' : '上门服务';
            $returnArr[] = array("content" => "发布者'" . $name . "' 在[" . $v->infoClass->name . "]" . $message . ",被 '" . $bname . "'  完成");
            // $returnArr[]=array("content"=>"发布者'".$name."' 在[".$v->infoClass->name."]".$message.",被 '".$bname."'  完成");
        }
        return $returnArr;
    }

    /**
     * 给订单进行评论
     * @param $data
     * @return mixed|void
     */
    public function setOrderInfoComment($data)
    {
        $orderInfo = $this->getInfoModel()->newQuery()->where("order_sn", $data['order_sn'])->first();

        if (empty($orderInfo)) {   //查无订单
            throw  new FreeBirdException(FreeBirdException::EROOR_INFO_NOT_FIND);
        }
        if ($orderInfo->uid != AUID) {  //订单不是自已无法评论
            throw new FreeBirdException(FreeBirdException::EROOR_INFO_ORDER_OWER);
        }
        //取得用户积分
        $userInfo = $this->getUserInterface()->getUserInfoById($orderInfo->buid); //查出用户评价用户
        if (empty($userInfo)) {
            throw  new FreeBirdException(FreeBirdException::EROOR_INFO_ACCEPT_UID_NOT_FIND);  //查不到接到用户。。系统异常
        }
        //请询用户是否已评论如果已评论
        $isExits = $this->getInfoCommentModelModel()->newQuery()->where("order_sn", $orderInfo->order_sn)->exists();
        if ($isExits) {
            throw  new FreeBirdException(FreeBirdException::EROOR_INFO_COMMENT_IS_EXITS);
        }
        $mdata = array();
        $mdata["comment_score"] = ($userInfo->comment_score + $data['score']) / 2;
        if ($this->getUserInterface()->updateXiaoUserInfo(["uid" => $orderInfo->uid], $mdata)) {   //更新用户平均值
            $data["uid"] = AUID;
            $data["buid"] = $orderInfo->buid;
            return $this->getInfoCommentModelModel()->newQuery()->create($data);
        }
    }

    /**
     * 取消订单
     * @param \App\FreeBird\Contracts\条件 $where
     * @return mixed|void
     */
    public function cancelOrder($where)
    {
        $info = $this->getInfoModel()->newQuery()->where($where)->first();
        if (empty($info)) {
            throw  new FreeBirdException(FreeBirdException::EROOR_INFO_NOT_FIND);
        }
        if ($info->order_status != '10' && $info->order_status != '20') {
            throw  new FreeBirdException(FreeBirdException::EROOR_INFO_STATUS);
        }
        if ($info->order_status == '20') {
            //把用户钱退到余额
            $this->getUserInterface()->updateUserBalance($info->uid, $info->all_amount, 6, $info->order_sn, '订单退款');
        }
        return $this->getInfoModel()->newQuery()->where($where)->update(["order_status" => '0']);
    }

    /**
     * 用户接单  ---入驻会员
     * @param $where
     * @return mixed|void
     */
    public function acceptOrder($where)
    {
        $info = $this->getInfoModel()->newQuery()->where($where)->first();
        if (empty($info)) {
            throw  new FreeBirdException(FreeBirdException::EROOR_INFO_NOT_FIND);
        }
        if ($info->order_status != '20') {
            throw  new FreeBirdException(FreeBirdException::EROOR_INFO_STATUS_EXEC);
        }
        if ($info->uid == AUID) {
            throw new FreeBirdException(FreeBirdException::EROOR_INFO_ACCEPT_SELF);
        }
        $data = array();
        $data["order_sn"] = $info->order_sn;
        $data["uid"] = $info->uid;
        $data["buid"] = AUID;

        //消费通知
        $mdata = array();
        $mdata["title"] = "接单通知";
        $mdata["content"] = "'" . $info->busername . "'接了你发布的订单，请尽快与对方联系。";
        $mdata["linkid"] = $info->order_sn;
        $mdata["uid"] = $info->uid;
        $this->getMessageModel()->newQuery()->create($mdata);


        return $this->getInfoAcceptlogModel()->newQuery()->create($data);
    }

    /**
     * 拒绝接单人
     * @param $where
     */
    public function refuseOrder($where)
    {
        $info = $this->getInfoAcceptlogModel()->newQuery()->where($where)->first();
        if (empty($info)) {
            throw  new FreeBirdException(FreeBirdException::EROOR_INFO_ACCEPT_NOT_EXITS);
        }
        return $this->getInfoAcceptlogModel()->newQuery()->where($where)->update(['apply_status' => -1]);
    }

    /**
     * 同意接单人
     * @param $where
     * @return mixed|void
     */
    public function agreeOrder($where)
    {
        $info = $this->getInfoAcceptlogModel()->newQuery()->where($where)->first();
        if (empty($info)) {
            throw  new FreeBirdException(FreeBirdException::EROOR_INFO_ACCEPT_NOT_EXITS);
        }
        if (!$this->getInfoModel()->newQuery()->where("order_sn", $info->order_sn)->update(["order_status" => '30', 'buid' => $info->buid])) {
            throw  new FreeBirdException(FreeBirdException::EROOR_INFO_UPDATE_ORDERSTATUS);
        }
        $mdata = array();
        $mdata["title"] = "同意接单通知";
        $mdata["content"] = "'" . $info->username . "'同意了你的接单请求，请尽快与对方联系。";
        $mdata["linkid"] = $info->order_sn;
        $mdata["uid"] = $info->buid;
        $this->getMessageModel()->newQuery()->create($mdata);
        return $this->getInfoAcceptlogModel()->newQuery()->where("id", $info->id)->update(["apply_status" => 1]);

    }

    /**
     * 取消接单
     * @param $where
     * @return mixed|void
     */
    public function cancelAccept($where)
    {
        $info = $this->getInfoModel()->newQuery()->where($where)->first();
        if (empty($info)) {
            throw  new FreeBirdException(FreeBirdException::EROOR_INFO_NOT_FIND);
        }
        $this->getInfoAcceptlogModel()->newQuery()->where("order_sn", $info->order_sn)->update(['apply_status' => -1]);
        return $this->getInfoModel()->newQuery()->where("id", $info->id)->update(['buid' => null]);
    }


    /**
     * 用户退单
     * @param $where
     */
    public function backOrder($where)
    {
        $info = $this->getInfoModel()->newQuery()->where($where)->first();
        if (empty($info)) {
            throw  new FreeBirdException(FreeBirdException::EROOR_INFO_NOT_FIND);
        }
        //不是30状态报错
        if ($info->order_status != '30') {
            throw  new FreeBirdException(FreeBirdException::EROOR_INFO_STATUS_EXEC);
        }

        $mdata = array();
        $mdata["title"] = "退单通知";
        $mdata["content"] = "'" . $info->username . "'退了你接的订单，请尽快与对方联系。";
        $mdata["linkid"] = $info->order_sn;
        $mdata["uid"] = $info->buid;
        $this->getMessageModel()->newQuery()->create($mdata);

        return $this->getInfoModel()->newQuery()->where("id", $info->id)->update(["order_status" => '35']);
    }

    /**
     * 打赏金币
     * @param $where
     * @return mixed|void
     */
    public function rewardOrder($where, $mount)
    {
        $info = $this->getInfoModel()->newQuery()->where($where)->first();
        if (empty($info)) {
            throw  new FreeBirdException(FreeBirdException::EROOR_INFO_NOT_FIND);
        }
        $paySn = $this->makePaySn(AUID);
        $openId = $this->getUserInterface()->getOpenIdByUid(AUID);
        $notify_url = get_config_param_value('XIAO_WXPAY_BING_NOTIFY');

        $payData = array();
        $payData["pay_sn"] = $paySn;
        $payData["order_sn"] = $info->order_sn;
        $payData["pay_amount"] = $mount;
        $payData["uid"] = AUID;
        $payData["type"] = 2; //打赏支付
        $this->getInfoPayLogModel()->newQuery()->create($payData);
        //不是30状态报错
        return $this->getDbWeixinPayRepositories()->getWeixinJsPay($paySn, $mount, $openId, $notify_url, "打赏支付");
    }


    /**
     * 完成按钮--入驻会员
     * @param $where
     */
    public function completionOrder($where)
    {

        $info = $this->getInfoModel()->newQuery()->where($where)->first();
        if (empty($info)) {
            throw  new FreeBirdException(FreeBirdException::EROOR_INFO_NOT_FIND);
        }
//        if($info->order_status!='30'){
//            throw  new FreeBirdException(FreeBirdException::EROOR_INFO_STATUS_EXEC);
//        }
//        if($info->uid==AUID){
//            throw new FreeBirdException(FreeBirdException::EROOR_INFO_ACCEPT_SELF);
//        }
        return $this->getInfoModel()->newQuery()->where("id", $info->id)->update(["order_status" => '40']);
    }

    /**
     * 放弃按钮---入驻会员
     * @param $where
     * @return mixed|void
     */
    public function giveupOrder($where)
    {
        $info = $this->getInfoModel()->newQuery()->where($where)->first();
        if (empty($info)) {
            throw  new FreeBirdException(FreeBirdException::EROOR_INFO_NOT_FIND);
        }
        $mdata = array();
        $mdata["title"] = "放弃通知";
        $mdata["content"] = "'" . $info->username . "'放弃了你发布的订单，请尽快与对方联系。";
        $mdata["linkid"] = $info->order_sn;
        $mdata["uid"] = $info->uid;
        $this->getMessageModel()->newQuery()->create($mdata);
        return $this->getInfoModel()->newQuery()->where($where)->update(["order_status" => '36']);
    }


    /***
     * 关闭订单
     * @param $where
     * @return int
     * @throws FreeBirdException
     */
    public function closeOrder($where)
    {
        $info = $this->getInfoModel()->newQuery()->where($where)->first();
        if (empty($info)) {
            throw  new FreeBirdException(FreeBirdException::EROOR_INFO_NOT_FIND);
        }
        return $this->getInfoModel()->newQuery()->where($where)->update(["order_status" => '45']);
    }


    /**
     * 订单起诉
     * @param $where
     * @return mixed|void
     */
    public function complaintOrder($where, $data)
    {
        unset($data['order_id']);
        $info = $this->getInfoModel()->newQuery()->where($where)->first();
        if (empty($info)) {
            throw  new FreeBirdException(FreeBirdException::EROOR_INFO_NOT_FIND);
        }
        $data["order_sn"] = $info->order_sn;
        $data["plaintiff_uid"] = AUID;  //起诉用户


        if ($info->uid == AUID) {
            $mdata = array();
            $mdata["title"] = "投诉通知";
            $mdata["content"] = "'" . $info->username . "'投诉了你接的订单，请尽快与对方联系。";
            $mdata["linkid"] = $info->order_sn;
            $mdata["uid"] = $info->buid;
            $this->getMessageModel()->newQuery()->create($mdata);
        }
        if ($info->buid == AUID) {
            $mdata = array();
            $mdata["title"] = "投诉通知";
            $mdata["content"] = "'" . $info->busername . "'投诉了你接的订单，请尽快与对方联系。。";
            $mdata["linkid"] = $info->order_sn;
            $mdata["uid"] = $info->uid;
            $this->getMessageModel()->newQuery()->create($mdata);
        }
        return $this->getInfoComplaintInterface()->updateAndCreate($data);
    }


    /**
     * 投诉订单
     * @param $where
     * @param $data
     * @return mixed|void
     */
    public function appealOrder($where, $data)
    {
        $info = $this->getInfoComplaintInterface()->findInfo($where);
        if (empty($info)) {
            throw  new FreeBirdException(FreeBirdException::EROOR_COMPLAINT_NOT_FIND);
        }
        //如果要加判断则，跟据launch_type类型查询订单是否存在:如下
//        $where=function($query) use ($info){
//            if($info->launch_type==1){
//                $query->where("uid",AUID);
//            }else if($info->launch_type==2){
//                $query->where("buid",AUID);
//            }
//            $query->where("order_sn",$info->order_sn);
//        };

        $data["order_sn"] = $info->order_sn;
        $data["accused_uid"] = AUID;
        $data["status"] = 1;  //后台显示与隐藏
        return $this->getInfoComplaintInterface()->updateAndCreate($data);

    }

    /**
     * 确认完成订单
     * @param $where
     * @return mixed|void
     */
    public function confirmationCompletedOrder($where)
    {
        $info = $this->getInfoModel()->newQuery()->where($where)->first();
        if (empty($info)) {
            throw  new FreeBirdException(FreeBirdException::EROOR_INFO_NOT_FIND);
        }
        if ($info->order_status == '40') {
            throw  new FreeBirdException(FreeBirdException::EROOR_INFO_UPDATE_ORDERSTATUS);
        }
        if ($this->getInfoModel()->newQuery()->where("order_sn", $info->order_sn)->update(["order_status" => "50"])) {
            //用户余额操作
            return $this->getInfoModel()->setUserBalance($info);
        }
    }

    /**
     * 判断定单是否存在
     * @param $where
     * @return bool|mixed
     */
    public function exitsOrder($where)
    {
        return $this->getInfoModel()->newQuery()->where($where)->first();
    }

    /**
     * 支付返回通知
     * @param $xml
     * @return mixed|void
     */
    public function payNotify($xml)
    {
        $returnData = $this->getDbWeixinPayRepositories()->checkNotity($xml);
        $info = $this->getInfoPayLogModel()->newQuery()->where("pay_sn", $returnData["out_trade_no"])->where("order_status", "10")->first();


        if (empty($info)) {
            //无此订单不用在响应
            throw  new FreeBirdException(FreeBirdException::EROOR_INFO_NOT_FIND);
        }
        if ($info->type == 1) {  //求邦支付
            //把订单的支付状态改成已支付
            if ($this->getInfoModel()->newQuery()->where("order_sn", $info->order_sn)->update(["order_status" => '20'])) {
                return $this->getInfoPayLogModel()->newQuery()->where("pay_sn", $returnData["out_trade_no"])->update(["order_status" => '20']);
            }
        } else if ($info->type == 2) {  //打赏支付
            //修改总金额与打赏金赏
            if ($this->getInfoModel()->newQuery()->where("order_sn", $info->order_sn)->update(["reward_amount" => DB::raw("reward_amount+" . $info->pay_amount), "all_amount" => DB::raw("all_amount+" . $info->pay_amount)])) {
                return $this->getInfoPayLogModel()->newQuery()->where("pay_sn", $returnData["out_trade_no"])->update(["order_status" => '20']);
            }
        }
        return false;
        Log::error("Pay......setVipBuyNotify..." . $xml);  //记录通知错误信息
    }


    /**
     * 修改订单审核状态
     * @param $where
     * @param $data
     * @return mixed|void
     */
    public function updateUserInfoTrueApply($where, $data)
    {
        $info = $this->getInfoModel()->newQuery()->where($where)->first();
        if (empty($info)) {
            throw  new FreeBirdException(FreeBirdException::EROOR_INFO_NOT_FIND);
        }
//        print_r($info->order_type);
//        exit;
        //自动接单
        $buid=$this->getAutomaticInterface()->autoAcceptOrder($info);
        if ($info->order_type == 2) {  //预约类型才发送消息
            if(empty($buid)){
                $mdata = array();
                $mdata["title"] = "接单通知";
                $mdata["content"] = "'" . get_wxusername_by_uid($buid) . "'接了你发布的订单，请尽快与对方联系。";
                $mdata["linkid"] = $info->order_sn;
                $mdata["uid"] = $info->uid;
                $this->getMessageModel()->newQuery()->create($mdata);
            }
        }
        // $this->getAutomaticInterface()->
        return $this->getInfoModel()->newQuery()->where($where)->update($data);
    }

    /**
     * 错误申请
     * @param $where
     * @param $data
     * @return int|mixed
     */
    public function updateUserInfoApply($where, $data)
    {
        if ($data["apply_status"] == -1) {  //拒绝发送通知
            $request = Request::instance();
            $id = $request->input("id");
            $info = $this->getInfoModel()->newQuery()->where("id", $id)->first();
            $mdata = array();
            $mdata["title"] = "审核未通过通知";
            $mdata["content"] = "你发布的订单未通过审核，请查看未通过原因并修改发布内容。";
            $mdata["linkid"] = $info->order_sn;
            $mdata["uid"] = $info->uid;
            $this->getMessageModel()->newQuery()->create($mdata);
        }
        return $this->getInfoModel()->newQuery()->where($where)->update($data);
    }

    /**
     * 获取订单信息
     * @param $id
     */
    public function getInfoMemberById($id)
    {
        return $this->getInfoModel()->newQuery()->with(["makeOrderUser"])->where("id", $id)->first();
    }

    /**
     * 生成支付单编号(两位随机 + 从2000-01-01 00:00:00 到现在的秒数+微秒+会员ID%1000)，该值会传给第三方支付接口
     * 长度 =2位 + 10位 + 3位 + 3位  = 18位
     * 1000个会员同一微秒提订单，重复机率为1/100
     * @return string
     */
    function makePaySn($member_id)
    {
        return mt_rand(10, 99)
            . sprintf('%010d', time() - 946656000)
            . sprintf('%03d', (float)microtime() * 1000)
            //. sprintf('%03d', (int) $member_id % 1000);
            . $member_id;
    }

    /**
     * 订单编号生成规则，n(n>=1)个订单表对应一个支付表，
     * 生成订单编号(年取1位 + $pay_id取13位 + 第N个子订单取2位)
     * 1000个会员同一微秒提订单，重复机率为1/100
     * @param int $pay_id 支付表自增ID
     * @return string
     */
    function makeOrderSn($pay_id)
    {
        //记录生成子订单的个数，如果生成多个子订单，该值会累加
        static $num;
        if (empty($num)) {
            $num = 1;
        } else {
            $num++;
        }
        return (date('y', time()) % 9 + 1) . $pay_id . sprintf('%01d', $num);
    }
}